package com.imoblife.brainwavestus;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

import com.imoblife.brainwavestus.R;
import com.imoblife.brainwavestus.adapter.AllProductCategoryAdapter;
import com.imoblife.brainwavestus.customview.AbsProductList;
import com.imoblife.brainwavestus.customview.AbsProductList.OnItemListener;
import com.imoblife.brainwavestus.listener.OnCategoryItemClickListener;
import com.imoblife.brainwavestus.model.Constant;
import com.imoblife.brainwavestus.product.Product;
import com.imoblife.brainwavestus.product.Store;
import com.imoblife.brainwavestus.user.User;
import com.imoblife.brainwavestus.util.BitMapFlyweightFactory;
import com.imoblife.brainwavestus.util.DebugUtil;

import android.app.Activity;
import android.content.Intent;
import android.database.Cursor;
import android.graphics.drawable.BitmapDrawable;
import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.view.ViewGroup.LayoutParams;
import android.widget.Adapter;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.ListAdapter;
import android.widget.ListView;
import android.widget.PopupWindow;
import android.widget.TextView;

public class LibraryActivity extends BaseActivity {
	
	private ListView mLvLocalLib;
	private ArrayAdapter<String> mCategoryAdapter;
	private Activity mActivity;
	private ArrayList<String> mCategories; 
	private User mUser;
	private Store mStore;
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		
		mActivity = this;
		BrainWavestusApplication app = getBrainWavesApplication();
		
		mUser = app.getUser();
		mStore = app.getStore();
		
		initCategoryDatas();
		
		if (LaunchScreenActivity.IS_PAD) {
			
			setContentView(R.layout.pad_category_product_list);
			initializeXLargerCategoryList();
		} else {
			
			Drawable back = this.getResources().getDrawable(R.drawable.back);
			View view = null;
			view = LayoutInflater.from(this).inflate(
					R.layout.phone_category_product_list, null);
			view.setBackgroundDrawable(back);
			setContentView(view);
			init();	
		}
			
	}
	
	public void initCategoryDatas(){
		
		mCategories = 
				new ArrayList<String>(mUser.getLocalLib().getLocalCategory());
		
		mCategories.add(0, getString(R.string.all_category));
		
		for(String category : mCategories){
			
			DebugUtil.dbDebug(TAG, "local category:" + category);
		}
	}
	
	public void init() {
		
		initTitleName(getString(R.string.library));
		
		mLvLocalLib = (ListView)findViewById(R.id.lv_category_product);
		
		AllProductCategoryAdapter adapter = 
				new AllProductCategoryAdapter(this, R.layout.category_item);
		
		adapter.setData(mCategories);
		
		mLvLocalLib.setAdapter(adapter);
		mLvLocalLib.setOnItemClickListener(
				new OnCategoryItemClickListener(this, 
						mCategories,
						Constant.SOURCE_FROM_LOCAL));
	}
	@Override
	protected void onDestroy() {
		
		super.onDestroy();
	}

	@Override
	protected void onPause() {
		
		super.onPause();
	}

	@Override
	protected void onRestoreInstanceState(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onRestoreInstanceState(savedInstanceState);
	}

	@Override
	protected void onResume() {
		// TODO Auto-generated method stub
		super.onResume();
	}

	@Override
	protected void onSaveInstanceState(Bundle outState) {
		// TODO Auto-generated method stub
		super.onSaveInstanceState(outState);
	}
	
	
	Cursor mProductCursor;// 产品游标

	AbsProductList mAbsProductList;// 产品列表

	HashMap<String, String> mtitleBuffer = new HashMap<String, String>();
	
	private String mCurrentCategory;
	
	protected void initializeXLargerCategoryList() {

		mCategoryAdapter = new ArrayAdapter<String>(this,
				R.layout.tb_category_item) {

			public View getView(int position, View convertView, ViewGroup parent) {

				TextView button;
				if (convertView == null) {

					button = (TextView) mActivity.getLayoutInflater().inflate(
							R.layout.tb_category_item, parent, false);

				} else {
					button = (TextView) convertView;
				}

				button.setText(getItem(position));
				return button;
			}

		};

		mCategoryAdapter.addAll(mCategories);

		final Button spinner = (Button) this.findViewById(R.id.categoryLayout);

		spinner.setText((String) mCategoryAdapter.getItem(0));
		spinner.setVisibility(View.VISIBLE);

		spinner.setOnClickListener(new OnClickListener() {

			public void onClick(View v) {
				initCategoryButton(spinner);
			}
		});
		mCurrentCategory = mCategories.get(0);
		initializeXlargerProductList();

	}
	
	protected void initializeXlargerProductList() {
		mAbsProductList = (AbsProductList) mActivity
				.findViewById(R.id.product_list);
		mAbsProductList.release();
		mAbsProductList.setVisibility(View.VISIBLE);
		long prev = System.currentTimeMillis();
		
		List<String> list = new ArrayList<String>();
		
		ArrayList<Product> products 
			= mUser.getLocalLib().getProductByCategory(mCurrentCategory, mStore);
		
		for(Product product : products) {
			
			String pId = product.getProductInfo().getId();
			list.add(pId);
			DebugUtil.debug(TAG, "add pad product Id: " + pId);
		}
		
		mAbsProductList.setProductLayout(R.layout.tb_product_icon);

		prev = System.currentTimeMillis();

		mAbsProductList.setDataList(list);

		mAbsProductList.setOnItemClicker(new OnItemListener() {

			public void onItemClick(View view, String productId) {

				Intent intent = new Intent(mActivity, UserProductActivity.class);
				intent.putExtra(Constant.PRODUCT_ID, productId);
				mActivity.startActivity(intent);
			}

			public void onItemLoading(View parent, String productId) {

				ImageView productIcon = (ImageView) parent
						.findViewById(R.id.product_icon);

				productIcon.setImageBitmap(BitMapFlyweightFactory
						.getBitMapFlyweightFactory(mActivity.getAssets())
						.getBitMapFlyweight(productId).getBitmap());

				LayoutParams params = productIcon.getLayoutParams();

				if (parent.getLayoutParams() != null) {
					params.width = parent.getLayoutParams().height
							- parent.getPaddingTop()
							- parent.getPaddingBottom();
					params.width *= 0.8;
					params.height = params.width;
					productIcon.setLayoutParams(params);
				}

				TextView productName = (TextView) parent
						.findViewById(R.id.product_name);
				String title = mStore.getProductByName(productId)					
						.getProductInfo().getTitle();
				productName.setText(title);
			}
		});
		System.gc();
	}
	

	private void initCategoryButton(final Button view) {
		View rootView = mActivity.getLayoutInflater().inflate(
				R.layout.popwindow, null);
		ListView listView = (ListView) rootView.findViewById(R.id.mylist);
		listView.setAdapter((ListAdapter) mCategoryAdapter);
		final PopupWindow popupWindow = new PopupWindow(view.getWidth() - 2,
				LayoutParams.WRAP_CONTENT);
		popupWindow.setFocusable(true);
		popupWindow.setContentView(rootView);
		popupWindow.setBackgroundDrawable(new BitmapDrawable());

		popupWindow.showAsDropDown(view, 1, 0);
		listView.setOnItemClickListener(new OnItemClickListener() {

			public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
					long arg3) {
				mCurrentCategory = mCategories.get(arg2);
				initializeXlargerProductList();
				view.setText((String) mCategoryAdapter.getItem(arg2));
				popupWindow.dismiss();
			}
		});

	}
}
